<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="boxdq">
          <div></div>
          <el-button
            type="primary"
            @click="(addedFormVisible = true), (type = 0)"
            >新增角色</el-button
          >
        </div>
        <div class="main_table" style="min-height: 690px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column label="序号" width="200">
              <template slot-scope="scope">{{
                (current.page - 1) * current.per_page + scope.$index + 1
              }}</template>
            </el-table-column>
            <el-table-column
              label="角色名称"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="角色描述"
              prop="remark"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="250"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <router-link
                  :to="scope.row.id != 1 ? '/global/grant/' + scope.row.id : ''"
                >
                  <el-link :type="scope.row.id != 1 ? 'warning' : 'info'"
                    >授权</el-link
                  >
                </router-link>
                <el-link
                  :type="scope.row.company_id != 0 ? 'primary' : 'info'"
                  @click="
                    scope.row.company_id != 0
                      ? ((addedFormVisible = true),
                        (type = 1),
                        (id = scope.row.id),
                        edit_assig(scope.$index))
                      : ''
                  "
                  >编辑</el-link
                >
                <el-link
                  :type="scope.row.company_id != 0 ? 'danger' : 'info'"
                  @click="
                    (id = scope.row.id),
                      delet(scope.$index, scope.row.company_id)
                  "
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[10,20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="type == 1 ? '编辑角色' : '新增角色'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog550"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="角色名称：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="角色名称"
            clearable
            class="wid350"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述：" prop="remark">
          <el-input
            v-model.trim="info.remark"
            placeholder="角色描述"
            clearable
            class="wid350"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  role_index,
  role_add,
  role_edit,
  role_delete,
} from "@/api/global/globalRole";
import rules from "@/rules/global/globalRole";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  components: {},
  data() {
    return {
      // 增加和编辑
      info: {
        name: "",
        remark: "",
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        page_size: 10,
      },
      limit: 10,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        name: "",
        remark: "",
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 删除
    delet(index, company_id) {
      if (company_id == 0) {
        return true;
      }
      let data = Object.assign(
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除角色?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          role_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          console.log(11111);
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        name: this.list.list.data[index].name,
        remark: this.list.list.data[index].remark,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        apiUrl().name,
        this.info
      );
      role_add(data).then((res) => {
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.role_index();
          this.addedFormVisible = false;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let data = Object.assign(
        apiUrl().name,
        this.info,
        { id: this.id }
      );

      role_edit(data).then((res) => {
        console.log("res", res);
        if (res.errcode == 0) {
          console.log("3333");
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.role_index();
          this.addedFormVisible = false;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.page_size = pageSize;
      this.role_index();
    },
    changePage(page) {
      this.current.page = page;
      this.role_index();
    },
    // 列表
    role_index() {
      this.listLoading = true;
      let data = Object.assign(
        apiUrl().name,
        this.current
      );
      role_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.role_index();
  },
};
</script>
<style lang="scss" scoped>
</style>
